#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
import os
from typing import NamedTuple


class ClassInfo(NamedTuple):
    name: str
    description: str
    tag: str
    consume: str
    produce: str


file_list = list()


def find_file(dir_path, custom_filter):
    if not os.path.exists(dir_path):
        print("path is not existed")
    if os.path.isfile(dir_path):
        if custom_filter(dir_path):
            file_list.append(dir_path)
    else:
        for sub_path in os.listdir(dir_path):
            find_file(os.path.join(dir_path, sub_path), custom_filter)
    return file_list


def file_filter(file_name):
    return file_name.endswith('actor.py')


def filter_content(files):
    actor_list = list()
    for file in files:
        name = ""
        description = ""
        tags = ""
        consumes = ""
        produces = ""
        with open(file, 'r') as f:
            is_description = False
            for line in f.readlines():
                line = line.strip('/n')
                if line.strip().startswith('class '):
                    name = line
                if is_description or line.strip().startswith('description = '):
                    description = description + "\n" + line
                if line.strip().startswith('\"\"\"'):
                    if is_description:
                        is_description = False
                    else:
                        is_description = True
                if line.strip().startswith('tags = '):
                    tags = line.split('(')[-1].split(')')[0]
                if line.strip().startswith('consumes = '):
                    consumes = line.split('(')[-1].split(')')[0]
                if line.strip().startswith('produces = '):
                    produces = line.split('(')[-1].split(')')[0]
        actor_list.append(ClassInfo(name, description, tags, consumes, produces))
    return actor_list


if __name__ == '__main__':
    dir_path = "D:/Work/GitHub/leapp-repository/repos/system_upgrade/el7toel8/actors"
    file_list = find_file(dir_path, file_filter)
    content_list = filter_content(file_list)
    for actor in content_list:
        with open("D:/Temp/5.17/actors.txt", 'a') as f:
            f.write(str(actor))
